package toubiao.controller;

import java.rmi.UnexpectedException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;








import toubiao.domain.po.manage.PoResource;
import toubiao.domain.vo.assist.Json;
import toubiao.domain.vo.assist.SessionInfo;
import toubiao.domain.vo.manage.User;
import toubiao.exception.CustomException;
import toubiao.service.ResourceServiceI;
import toubiao.utils.ProUtilConfig;
import utils.UtilBean;
import utils.UtilController;
import utils.UtilJackson;
import utils.easyui.domain.EzTree;

@Controller
@RequestMapping("/layout")
public class LayoutController {

	private static Logger logger=LoggerFactory.getLogger(LayoutController.class);
	
	@Autowired
	private ResourceServiceI resourceService;
	
	@RequestMapping("/west.subJsp")
	public String subJspGetWest(HttpServletRequest request){
		logger.info("in subJsp");
		return "/module/layout/west";
	}
	
	@RequestMapping("/south.subJsp")
	public String subJspGetSouth(HttpServletRequest request){
		
		return "/module/layout/south";
	}
	
	@RequestMapping("/center.jspPage")
	public String subJspGetCenter(HttpServletRequest request){
		return "/module/infom/infom";
	}
	
	@RequestMapping("/west/accordionPanel.json")
	@ResponseBody
	public Json jsonGetAccordionPanel(HttpSession session,HttpServletRequest request){
		Json json=new Json();
		List<PoResource> list=null;
		SessionInfo sessionInfo=(SessionInfo) session.getAttribute(ProUtilConfig.getSessionInfoName());
		int userId=sessionInfo.getUserId();
		User user =new User();
		user.setId(userId);
		String url=request.getServletPath();
		logger.info("servletPath = " +url);
		try {
			list=resourceService.getResourceListByUserAndUrl(user, url);
			logger.info(list.size()+"");
			String[] propertyArrayStrings=new String[]{"name","id","url","icon"};
			list=UtilBean.getCopyInludeProperties(PoResource.class, list, propertyArrayStrings);
			logger.info(list.size()+"");
		} catch (CustomException | IllegalArgumentException | IllegalAccessException |
				InstantiationException | SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.setMessage(e.getMessage());
			return json;
		} 
		json.setSuccess(true);
		json.setData(list);
		return json;
	}
	
	@RequestMapping("/west/accordionPanel/menuTree.json")
	@ResponseBody
	public List<EzTree> jsonGetMenuTreeByPanelId(int panelId){
		logger.info(panelId+"");
		PoResource panelResource=resourceService.getPoResouceById(panelId);
		Set<PoResource> menuPoResources=panelResource.getChildrenSet();
		logger.info(menuPoResources.size()+"");
		List<PoResource> list=new ArrayList<>();
		list.addAll(menuPoResources);
		List<EzTree> treeList=new ArrayList<>();
		try {
			logger.info(list.size()+"");
			treeList = resourceService.getResourceListAsEzTree(list);
		} catch (UnexpectedException | IllegalArgumentException
				| IllegalAccessException | NoSuchFieldException
				| SecurityException | InstantiationException | CustomException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return treeList;
		}
		logger.info(treeList.size()+"");
		return treeList;
	}
}
